﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Pasys.PresentationCore;
using Pasys.PresentationCore.WinForms;
using WeifenLuo.WinFormsUI.Docking;
using Pasys.PresentationCore.DockShell;
using Pasys.PresentationCore.DockShell.SmartPartInfos;

namespace WinFormShell
{
    public partial class ShellDockLayoutView : UserControl
    {


        public IShellLayoutViewProxy LayoutViewProxy { get; private set; }




        public ShellDockLayoutView()
        {
            InitializeComponent();

            dockPanelWorkspace.DocumentStyle = DocumentStyle.DockingWindow;
            dockPanelWorkspace.Dock = DockStyle.Fill;
            dockPanelWorkspace.DockTopPortion = 180;
            dockPanelWorkspace.DockBottomPortion = 180;
            dockPanelWorkspace.DockLeftPortion = 230;
            dockPanelWorkspace.DockRightPortion = 230;

            dockPanelWorkspace.ActiveContentChanged += ThisActiveContentChanged;

            LayoutViewProxy = new ShellLayoutViewProxy(this, Show)
            {
                MainMenuStrip = mainMenuStrip,
                MainStatusStrip = mainStatusStrip,
                StatusLabel = statusLabel,
                ToolStripContainer = toolStripContainer
            };

        }

        public IComposableWorkspace<Control, DockPanelSmartPartInfo> Workspace
        {
            get { return dockPanelWorkspace; }
        }


        private void ThisActiveContentChanged(object sender, EventArgs e)
        {
            //if (dockPanelWorkspace.ActiveSmartPart is Control)
            //{
            //    ((Control)dockPanelWorkspace.ActiveSmartPart).Focus();
            //}
        }

        //private Control GetSmartPart(IDockContent dockContent)
        //{
        //    // Locate the smart part corresponding to the dockContent.
        //    foreach (var pair in dockContents)
        //    {
        //        if (pair.Value == dockContent)
        //        {
        //            return pair.Key;
        //        }
        //    }

        //    // not found
        //    return null;
        //}



        /// <summary>
        /// Shows SmartPart using the given SmartPartInfo
        /// </summary>
        /// <param name="smartPart">Smart part to show.</param>
        /// <param name="smartPartInfo"></param>
        public void Show(IPersistViewProvider viewProvider)
        {
            var smartPartKey = viewProvider.View;
            var smartPartInfo = viewProvider.ViewInfo;

            if (smartPartKey == null)
            {
                return;
            }

            object smartPart = smartPartKey;
            int i = Workspace.SmartParts.IndexOf(smartPartKey);
            if (i >= 0)
            {
                smartPart = Workspace.SmartParts[i];
                if (smartPart != smartPartKey)
                {
                    var d = smartPartKey as IDisposable;
                    if (d != null)
                    {
                        d.Dispose();
                    }
                    smartPartKey = null;
                }
            }

            var viewInfo = smartPartInfo as ISmartPartInfo;
            if (viewInfo == null)
            {
                //Workspace.Show(smartPart);
                viewInfo = new DockPanelSmartPartInfo()
                {
                    DockingType = DockingType.Document,
                    ShowStrategy = new DockStateShowStrategy(Pasys.PresentationCore.DockShell.SmartPartInfos.DockState.Document),
                };
            }

            viewInfo.PersistStringCallback = viewProvider.GetPersistStringCallback();
            var f = smartPart as Form;
            var info = smartPartInfo as ISmartPartInfo;
            if (f != null && info != null)
            {
                if (string.IsNullOrEmpty(info.Title))
                {
                    info.Title = f.Text;
                }

                if (string.IsNullOrEmpty(info.Description))
                {
                    info.Description = f.Text;
                }
            }

            var iconInfo = smartPartInfo as IconSmartPartInfo;
            if (f != null && iconInfo != null)
            {
                if (iconInfo.Icon == null && f.Icon != null)
                {
                    iconInfo.Icon = f.Icon;
                }
            }

            Workspace.Show(smartPart, viewInfo);

        }
    }
}
